package com.example.photography.adapter.store;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.photography.R;
import com.example.photography.SeckillRecyclerViewAdapter;
import com.example.photography.bean.StoreHomeBean;
import com.example.photography.util.Util;
import com.youth.banner.Banner;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.holder.BannerImageHolder;
import com.youth.banner.indicator.RectangleIndicator;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.transformer.ZoomOutPageTransformer;
import com.youth.banner.util.BannerUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class StoreFragmentAdapter extends RecyclerView.Adapter {

    /**
     * 广告条幅
     */
    public static final int BANNER = 0;

    /**
     * 频道
     */
    public static final int CHANNEL = 1;
    /**
     * 活动
     */
    public static final int ACT = 2;
    /**
     * 秒杀
     */
    public static final int SECKILL = 3;
    /**
     * 推荐
     */
    public static final int RECOMMED = 4;
    /**
     * 热卖
     */
    public static final int HOT = 5;
    //类型
    private int currentType = 0;

    private Context context;
    private StoreHomeBean.ResultBean resultBean;
    /**
     * 初始化布局
     */
    private final LayoutInflater layoutInflater;

    public StoreFragmentAdapter(Context context, StoreHomeBean.ResultBean resultBean) {
        this.context = context;
        this.resultBean = resultBean;
        layoutInflater = LayoutInflater.from(context);
    }

    /**
     * 相当于getView 创建viewHolder部分代码
     *
     * @param parent
     * @param viewType 当前的类型
     * @return
     */
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == BANNER) {
            return new BannerViewHolder(context, layoutInflater.inflate(R.layout.item_banner, null));
        }else if (viewType == CHANNEL){
            return new ChannelViewHolder(context, layoutInflater.inflate(R.layout.channel_item, null));
        }else if (viewType == ACT){
            return new ActViewHolder(context, layoutInflater.inflate(R.layout.act_banner, null));
        }else if (viewType == SECKILL){
            return new SeckillViewHolder(context, layoutInflater.inflate(R.layout.seckill_item, null));
        }else if (viewType == RECOMMED){
            return new RecommendViewHolder(context, layoutInflater.inflate(R.layout.store_recommend_item, null));
        }else if (viewType == HOT){
            return new HotViewHolder(context, layoutInflater.inflate(R.layout.store_hot_item, null));
        }
        return null;
    }

    /**
     * 得到类型
     *
     * @param position
     * @return
     */
    @Override
    public int getItemViewType(int position) {
        switch (position) {
            case BANNER:
                currentType = BANNER;
                break;
            case CHANNEL:
                currentType = CHANNEL;
                break;
            case ACT:
                currentType = ACT;
                break;
            case SECKILL:
                currentType = SECKILL;
                break;
            case RECOMMED:
                currentType = RECOMMED;
                break;
            case HOT:
                currentType = HOT;
                break;
        }
        return currentType;
    }

    /***
     * 相当于getView中的绑定数据模块
     * @param holder
     * @param position
     */
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (getItemViewType(position) == BANNER){
            BannerViewHolder bannerViewHolder = (BannerViewHolder) holder;
            bannerViewHolder.setData(resultBean.getBanner_info());
        }else if (getItemViewType(position) == CHANNEL){
            ChannelViewHolder channelViewHolder = (ChannelViewHolder) holder;
            channelViewHolder.setData(resultBean.getChannel_info());
        }else if (getItemViewType(position) == ACT){
            ActViewHolder actViewHolder = (ActViewHolder) holder;
            actViewHolder.setData(resultBean.getAct_info());
        }else if (getItemViewType(position) == SECKILL){
            SeckillViewHolder seckillViewHolder = (SeckillViewHolder) holder;
            seckillViewHolder.setData(resultBean.getSeckill_info());
        }else if (getItemViewType(position) == RECOMMED){
            RecommendViewHolder recommendViewHolder = (RecommendViewHolder) holder;
            recommendViewHolder.setData(resultBean.getRecommend_info());
        }else if (getItemViewType(position) == HOT){
            HotViewHolder hotViewHolder = (HotViewHolder) holder;
            hotViewHolder.setData(resultBean.getHot_info());
        }

    }

    /**
     * 总共有多少个item类型
     * @return
     */
    @Override
    public int getItemCount() {
        return 6;
    }

    //广告轮转
    class BannerViewHolder extends RecyclerView.ViewHolder {
        private Context context;
        private Banner banner;


        public BannerViewHolder(Context context, View view) {
            super(view);
            this.context = context;
            this.banner = view.findViewById(R.id.banner);
        }

        public void setData(List<StoreHomeBean.ResultBean.BannerInfoBean> banner_info) {
            //设置Banner的数据
            /**
             * 获取轮转图片数据
             */
            banner.setAdapter(new BannerImageAdapter<StoreHomeBean.ResultBean.BannerInfoBean>(banner_info) {
                @Override
                public void onBindView(BannerImageHolder holder, StoreHomeBean.ResultBean.BannerInfoBean image, int position, int size) {
                    //图片加载自己实现
                    Glide.with(holder.itemView)
                            .load(image.getImage())
                            .thumbnail(Glide.with(holder.itemView).load(R.drawable.loading))
                            .apply(RequestOptions.bitmapTransform(new RoundedCorners(30)))
                            .into(holder.imageView);

                }
            });
           banner.setIndicator(new RectangleIndicator(context))//设置指示器
                    .setIndicatorSelectedWidth((int) BannerUtils.dp2px(12))
//                    .addBannerLifecycleObserver(this)//添加生命周期观察者(调用banner的addBannerLifecycleObserver()方法，让banner自己控制)
                    .setOnBannerListener(new OnBannerListener() {
                        @Override
                        public void OnBannerClick(Object data, int position) {
                            // TODO: 2020/10/19 点击事件有问题，循环三次后事件没反应
                            Util.show(context, String.valueOf(position));
                        }
                    });
        }
    }
    //频道
    class ChannelViewHolder extends RecyclerView.ViewHolder{
        private Context context;
        private GridView gridView;
        public ChannelViewHolder(Context context, View view) {
            super(view);
            this.context = context;
            gridView = view.findViewById(R.id.gv_channel);
            gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                    Util.show(context,"position="+position);
                }
            });
        }


        public void setData(List<StoreHomeBean.ResultBean.ChannelInfoBean> channel_info) {
            //装载适配器
            ChannelAdapter channelAdapter = new ChannelAdapter(channel_info,context);
            gridView.setAdapter(channelAdapter);

        }
    }
    //活动轮转(仿一号店)
    class ActViewHolder extends RecyclerView.ViewHolder{
        private Context context;
        private Banner banner;
        public ActViewHolder(Context context, View view) {
            super(view);
            this.context = context;
            banner = view.findViewById(R.id.banner);
        }

        public void setData(List<StoreHomeBean.ResultBean.ActInfoBean> act_info) {
            //实现1号店和淘宝头条类似的效果
            banner.setAdapter(new ActAdapter(act_info))
                    .setOrientation(Banner.VERTICAL)
                    .setPageTransformer(new ZoomOutPageTransformer())
                    .setOnBannerListener((data, position) -> {
                        Util.show(context,"position="+position);
                    });
        }
    }

    /**
     * 相差多少时间-毫秒
     */
    private long dt = 0;
    //秒杀
    class SeckillViewHolder extends RecyclerView.ViewHolder{

        private Context context;
        private TextView tv_time_seckill;
        private TextView tv_more_seckill;
        private RecyclerView rv_seckill;
        private SeckillRecyclerViewAdapter seckillRecyclerViewAdapter;

        public SeckillViewHolder(Context context, View view) {
            super(view);
            this.context = context;
            tv_time_seckill = view.findViewById(R.id.tv_time_seckill);
            tv_more_seckill = view.findViewById(R.id.tv_more_seckill);
            rv_seckill = view.findViewById(R.id.rv_seckill);


        }

        public void setData(StoreHomeBean.ResultBean.SeckillInfoBean seckill_info) {
            //1. 得到数据
            //2. 设置数据：文本和RecyclerView数据
            seckillRecyclerViewAdapter = new SeckillRecyclerViewAdapter(context,seckill_info.getList());
            rv_seckill.setAdapter(seckillRecyclerViewAdapter);

            //设置布局管理器
            rv_seckill.setLayoutManager(new LinearLayoutManager(context,LinearLayoutManager.HORIZONTAL,false));
            //设置item的点击事件
            seckillRecyclerViewAdapter.setOnseckillRecyclerView(new SeckillRecyclerViewAdapter.OnseckillRecyclerView() {
                @Override
                public void OnItemClick(int position) {
                    Util.show(context,"秒杀="+position);
                }
            });

            //秒杀倒计时-毫秒
            dt = Integer.valueOf(seckill_info.getEnd_time())-Integer.valueOf(seckill_info.getStart_time());


            handler.sendEmptyMessageDelayed(0,1000);


        }
        @SuppressLint("HandlerLeak")
        private Handler handler = new Handler(){
            @Override
            public void handleMessage(@NonNull Message msg) {
                super.handleMessage(msg);
                dt = dt - 1000;
                @SuppressLint("SimpleDateFormat") SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
                String time = format.format(new Date(dt));
                tv_time_seckill.setText(time);
                handler.removeMessages(0);
                handler.sendEmptyMessageDelayed(0,1000);
                if (dt <= 0){
                    //把消息移除
                    handler.removeCallbacksAndMessages(null);
                }
            }
        };
    }
    //新品推荐
    class RecommendViewHolder extends RecyclerView.ViewHolder{
        private Context context;
        private GridView gv_recommend;
        private TextView tv_more_recommend;
        public RecommendViewHolder(Context context, View view) {
            super(view);
            this.context = context;
            gv_recommend = view.findViewById(R.id.gv_recommend);
            tv_more_recommend = view.findViewById(R.id.tv_more_recommend);
            gv_recommend.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                    Util.show(context,"position="+position);
                }
            });
        }

        public void setData(List<StoreHomeBean.ResultBean.RecommendInfoBean> recommend_info) {
            //1. 获取数据
            //2. 设置适配器
            RecommendGridViewAdapter recommendGriViewAdapter = new RecommendGridViewAdapter(recommend_info,context);
            gv_recommend.setAdapter(recommendGriViewAdapter);
        }
    }
    class HotViewHolder extends RecyclerView.ViewHolder{
        private TextView tv_more_hot;
        private GridView gv_hot;
        private final Context context;

        public HotViewHolder(Context context, View view) {
            super(view);
            this.context = context;
            tv_more_hot = view.findViewById(R.id.tv_more_hot);
            gv_hot = view.findViewById(R.id.gv_hot);
            gv_hot.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                    Util.show(context,"position="+position);
                }
            });
        }

        public void setData(List<StoreHomeBean.ResultBean.HotInfoBean> hot_info) {
            //1. 得到数据
            //2. 装配适配器
            HotGridViewAdapter hotGridViewAdapter = new HotGridViewAdapter(hot_info,context);
            gv_hot.setAdapter(hotGridViewAdapter);
        }
    }

}
